<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use tests\Mockery\Adapter\Phpunit\EmptyTestCase;

/**
 * 用户模型
 * Class User
 * @package App\Models
 */
class User extends Model
{
    /**
     * 定义用户表名称
     * @var string
     */
    protected $table='user';
    /**
     * 定义用户表模型
     * @var
     */
    protected $userModel;
    /**
     * 定义模型字段属性
     */
    public $id           ='id';
    public $username     = 'username';
    public $password     = 'password';
    public $email        = 'email';
    public $image        = 'image';
    public $mobile       = 'mobile';
    public $sex          = 'sex';
    public $create_at    = 'created_at';
    public $updated_at   = 'updated_at';
    public $ip           = 'ip';
    public $is_open      = 'is_open';
    public $disabled     = 'disabled';

    /**
     * 表模型初始化
     * User constructor.
     * @param array $attributes
     */
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->userModel = DB::table($this->table);
    }

    /**
     * 用户列表
     */
    public function getList($params){

        if($params['getfield']){
            return  $this->userModel->where($params['where'])->orderBy($params['order'])->select($params['field'])->get()->toArray();
        }else{
           return $this->userModel->where($params['where'])->orderByDesc($params['order'])->offset($params['page'])->limit($params['limit'])->get($params['field']);
        }
    }

    /**
     * 用户数据统计
     * @param $params
     * @return int
     */
    public function getCount($params){
        return  $this->userModel->where($params['where'])->count();
    }

}
